<?php
namespace app\admin\controller\contents;
use app\common\controller\Backend;
class Yuyue extends Backend{

    protected $relationSearch = false;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('orders');
    }

    /**
     * 查看
     */
    public function index()
    {
        $input=input();
        $tp=$input['tp'] ?? 0;
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $newWhere = ['order_tp'=>$tp];
            $total = $this->model
                ->where($newWhere)
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($newWhere)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v['atime'] = date("Y-m-d H:i:s",$v['atime']);
                $v['order_state_desc']=order_state($v);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig("tp", $this->request->param("tp"));
        return $this->view->fetch();
    }

    public function fenpei($ids = NULL){
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('yg_id','sm_time'))
            ));
            if(empty($params['sm_time']) || strlen($params['sm_time'])<5){
                $this->error('请选择上门时间');
            }
            if ($params) {
                $params['order_state']=3;
                $params['sm_time']=strtotime($params['sm_time']);
                Model('orders')->where(array('id'=>$ids))->update($params);
                $url=$_SERVER['REQUEST_URI'].'&ok=1';
                header("location:$url");
                exit;
            }
            $this->error();
        }
        $row =Model('orders')->where(array('id'=>$ids))->find();
        if(empty($row)){
            $this->error('订单不存在');
        }
        $zidian=base_list();
        $row['yuyue_time']=date("Y-m-d H:i:s",$row['ydate']);
        $this->view->assign('zidian',$zidian);
        $yuangong=Model('yuangong')->where(array('tp'=>1))->select();
        $this->view->assign('yuangong',$yuangong);
        $this->view->assign('row',$row);
        if($row['order_tp']==1){
            $this->view->assign('goods_list',Model('orders_item')->where(array('order_id'=>$ids))->select());
        }
        return $this->view->fetch();
    }
    public function info($ids = NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('yg_id'))
            ));
            if ($params) {
                $params['order_state']=3;
                Model('orders')->where(array('id'=>$ids))->update($params);
                $this->success();
            }
            $this->error();
        }
        $row =Model('orders')->where(array('id'=>$ids))->find();
        if(empty($row)){
            $this->error('订单不存在');
        }
        $zidian=base_list();
        $row['yuyue_time']=date("Y-m-d",strtotime($row['ydate'])).' '.$row['yhour'].'时';
        $this->view->assign('zidian',$zidian);
        $yuangong=Model('yuangong')->select();
        $this->view->assign('yuangong',$yuangong);
        $this->view->assign('row',$row);
        if($row['order_tp']==1){
            $this->view->assign('goods_list',Model('orders_item')->where(array('order_id'=>$ids))->select());
        }
        if($row['yg_id']>0){
            $this->assign('yg_info',Model('yuangong')->where(array('id'=>$row['yg_id']))->find());
        }
        return $this->view->fetch();
    }
}
